package com.zbkc.main;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zbkc.bean.BeifenBean;
import com.zbkc.interfaces.SCInterface;
import com.zbkc.main.base.BaseTitleActivity;
import com.zbkc.util.AlertUtils;
import com.zbkc.util.MyApplication;
import com.zbkc.util.ThreadPoolFactory;

public class BeiFenDeviceActivity extends BaseTitleActivity implements
		AdapterView.OnItemClickListener {

	private ListView listView;
	private MyAdapter adapter;
	private List<BeifenBean> beanList;

	boolean isFirstBack = false;
	long firstBackT = 0L;

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				adapter = new MyAdapter(BeiFenDeviceActivity.this, 0, beanList);
				listView.setAdapter(adapter);
				break;
			}
			AlertUtils.dismissLoadingDialog();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_beifen_device);
		bindView();
		getData();
	}

	private void bindView() {
		setTitle("我的备份");
		setBtn1Visiable(false);
		setBtn2Visiable(false);

		listView = (ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(this);
	}

	private void getData() {
		AlertUtils.showLoadingDialog(this, "正在获取数据,请稍后...", true);
		ThreadPoolFactory.getInstance().execute(new Runnable() {
			public void run() {
				beanList = SCInterface.getDeviceList();
				handler.sendEmptyMessage(1);
			}
		});
	}

	public void onBackPressed() {
		if (!this.isFirstBack) {
			this.firstBackT = System.currentTimeMillis();
			Toast.makeText(this, "再按一次退出程序！", 0).show();
			this.isFirstBack = true;
			new Thread(new Runnable() {
				public void run() {
					try {
						Thread.sleep(5000L);
						BeiFenDeviceActivity.this.isFirstBack = false;
						return;
					} catch (InterruptedException localInterruptedException) {
					}
				}
			}).start();
			return;
		}
		MyApplication.getInstance().finishAllActivity();
		System.gc();
		System.exit(0);
		super.onBackPressed();
	}

	public void onItemClick(AdapterView<?> paramAdapterView, View paramView,
			int paramInt, long paramLong) {
		Intent intent = new Intent();
		intent.setClass(this, BeifenDirectoryActivity.class);
		intent.putExtra("id", beanList.get(paramInt).getId());
		startActivity(intent);
	}

	class MyAdapter extends ArrayAdapter<BeifenBean> {
		public MyAdapter(Context context, int textViewResourceId,
				List<BeifenBean> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int position, View paramView,
				ViewGroup paramViewGroup) {
			ViewHolder viewHolder;
			if (paramView == null) {
				viewHolder = new ViewHolder();
				paramView = View.inflate(getContext(),
						R.layout.listview_beifen_item, null);
				viewHolder.tvtitle = ((TextView) paramView
						.findViewById(R.id.tv_title));
				paramView.setTag(viewHolder);
			} else {
				viewHolder = ((ViewHolder) paramView.getTag());
			}

			viewHolder.tvtitle.setText(((BeifenBean) beanList.get(position))
					.getDeviceName());
			return paramView;
		}

		class ViewHolder {
			TextView tvtitle;
		}
	}

}
